package com.come4loves.game.module.actor.platform;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.come4loves.core.comunicate.AllowAccess;
import com.come4loves.core.result.TResult;
import com.come4loves.core.utils.HttpUtils;
import com.come4loves.game.constent.GameStatusCodeConstant;
@Component
public class PlatformInvokeImpl implements PlatformInvoke {
	private static final Logger LOGGER = LoggerFactory.getLogger(PlatformInvokeImpl.class);
	@Autowired
	@Qualifier("http.login.validate_url")
	private String url;
	
	@Autowired
	@Qualifier("allowAccesGamePortImpl")
	private AllowAccess allowAccess;
	@Override
	public TResult<PlatformLoginResult> login(Map<String,String> params) {
		String text = "";
		try {
			text = HttpUtils.sendPost(url, params);
			if (text != null) {
				PlatformJsonResult result =  JSON.parseObject(text, PlatformJsonResult.class);
				if (allowAccess.allowFlag(result.uid) == false) {
					return TResult.valueOf(GameStatusCodeConstant.TOKEN_VALIDATE_ERROR);
				}
				if (result.isOK()) {
					return TResult.sucess(PlatformLoginResult.valueOf(result.uid));
				} else {
					LOGGER.warn(String.format("url:[%s] suffix:[%s] http text:[%s]", url, params, text));
				}
			}
		} catch (Exception ex) {
			LOGGER.warn(String.format("url:[%s] params:[%s] http text:[%s]", url, params, text), ex);
		}
		return TResult.valueOf(GameStatusCodeConstant.TOKEN_VALIDATE_ERROR);
	}
	

}
